<!--
 * @Author: 45654 2275061717@qq.com
 * @Date: 2022-10-12 14:12:13
 * @LastEditors: 45654 2275061717@qq.com
 * @LastEditTime: 2022-10-17 15:49:38
 * @FilePath: \实训三\小组餐饮平台项目\team-cooperation-project\src\pages\admin\carousel\List.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <!-- 轮播配置页面 -->
  <div>
    <!-- 按钮 -->
    <el-button type="success" @click="toAdd">新增</el-button>
    <el-button type="success" @click="preview">预览</el-button>

    <!-- 表格区域 -->
    <el-table
      :data="carousels"
      style="width: 100%"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column prop="id" label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="轮播图名称" />
      <el-table-column prop="introduce" label="轮播图描述" />
      <el-table-column prop="url" label="轮播图图片" width="275">
        <template slot-scope="scope">
          <el-popover placement="right" trigger="hover">
            <img
              slot="reference"
              :src="scope.row.url"
              width="30px"
              alt="加载失败"
            >
            <img width="200px" height="200px" :src="scope.row.url" alt="">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status === '正常'"
            type="success"
          >正常</el-tag>
          <el-tag v-else type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <a href="#" class="toEdit" @click="toEdit(scope.row)">编辑</a>
          <a
            href="#"
            class="toDelete"
            @click.prevent="toDelete(scope.row.id)"
          >删除</a>
          <a
            v-if="scope.row.status === '正常'"
            href="#"
            class="toStop"
            @click.prevent="toStop(scope.row.id,scope.row.name,scope.row.introduce,scope.row.url,'停用')"
          >停用</a>
          <a
            v-else
            href="#"
            class="toStop"
            @click.prevent="toStop(scope.row.id,scope.row.name,scope.row.introduce,scope.row.url,'正常')"
          >启用</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域结束 -->

    <!-- 模态框区域 -->
    <el-dialog :title="title" :visible.sync="visible">
      <el-form ref="myForm" :model="form" :rules="rules">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <!-- 图片上传 -->
        <el-form-item label="轮播图片" :label-width="formLabelWidth" prop="url">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="form.url" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <!-- 图片上传结束 -->

        <!-- 多行文本框 -->
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input v-model="form.introduce" type="textarea" />
        </el-form-item>
        <!-- 多行文本框结束 -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose()">取 消</el-button>
        <el-button type="primary" @click="toSubmit()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框区域结束 -->

    <!-- 图片预览模态框 -->
    <el-dialog title="大屏预览" :visible.sync="picture" width="70%">
      <template>
        <el-carousel :interval="5000" arrow="hover" height="400px">
          <el-carousel-item v-for="item in newCarousels" :key="item.id">
            <h3><img :src="item.url" alt="加载失败"></h3>
          </el-carousel-item>
        </el-carousel>
      </template>
      <div slot="footer" class="dialog-footer">
        <el-button @click="picture = false">取 消</el-button>
        <el-button type="primary" @click="picture = false">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 图片预览模态框结束 -->
  </div>
</template>

<script>
import { get, post } from '@/utils/request'
import { uploadFileURL } from '@/utils/config'
export default {
  data() {
    return {
      // 轮播图变量
      carousels: [],
      // 模态框标题
      title: '',
      // 模态框显示与隐藏
      visible: false,
      picture: false,
      // 表单对象
      form: {},
      formLabelWidth: '120px',
      // 图片上传路径
      uploadFileURL,
      // 图片上传变量
      imageUrl: '',
      // 表单校验规则
      rules: {
        name: [
          { required: true, message: '请输入轮播图名称', trigger: 'blur' }
        ],
        url: [{ required: true, message: '请上传轮播图', trigger: 'change' }]
      }
    }
  },
  computed: {
    newCarousels() {
      return this.carousels.filter((item) => {
        if (item.status === '正常') {
          return item
        }
      })
    }
  },
  created() {
    this.getAllCarousel()
  },
  mounted() {},
  methods: {
    // 图片预览轮播
    preview() {
      this.picture = true
    },
    // 删除轮播图
    toDelete(id) {
      this.$confirm('此操作将永久删除该轮播图, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await get('/carousel/deleteById', { id })
          console.log(res, '查看')
          this.$message({
            type: 'success',
            message: res.message
          })
          // 刷新页面
          this.getAllCarousel()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭模态框回调
    toClose() {
      // 重置表单
      setTimeout(() => {
        this.$refs.myForm.resetFields()
        this.visible = false
      }, 500)
    },
    // 获取所有轮播图
    async getAllCarousel() {
      const res = await get('/carousel/findAll')
      this.carousels = res.data
      console.log(res.data)
    },
    // 新增按钮事件
    toAdd() {
      this.visible = true
      this.title = '新增轮播图'
      this.form = {}
    },
    // 图片上传成功方法
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.url = res.data
    },
    // 图片上传之前的方法
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },

    // 去编辑
    toEdit(row) {
      this.form = { ...row }
      this.title = '编辑轮播图'
      this.visible = true
      this.imageUrl='123'
    },
    toSubmit() {
      // 校验表单
      this.$refs.myForm.validate(async(valid) => {
        if (valid) {
          // 发送保存或者更新请求
          const res = await post('/carousel/saveOrUpdate', this.form)
          this.$notify({
            type: 'success',
            message: res.message
          })
          this.getAllCarousel()
          this.toClose()
        } else {
          return false
        }
      })
    },
    // 轮播图停用或者启用按钮
    toStop(id, name, introduce, url, status) {
      this.$confirm('此操作将停用该轮播图, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const tem = { id, name, introduce, url, status }
          const res = await post('/carousel/saveOrUpdate', tem)
          this.$message({
            type: 'success',
            message: res.message
          })
          this.getAllCarousel()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    }
  }
}
</script>
<style lang="scss">
$color: #e65d6e;
a {
  margin-right: 5px;
}
.toEdit {
  color: #30b08f;
}
.toDelete {
  color: $color;
}
.toStop {
  color: #3a71a8;
}

// 预览模态框样式

.el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 400px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.el-carousel__item h3 img {
  width: 100%;
  height: 400px;
}
</style>

